package com.link.base.base.system.service;

import com.link.base.base.system.dao.mybatis.mapper.DelBackupsMapper;
import com.link.base.base.system.model.DelBackups;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.BeanTools;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LiYang
 * @date 20190521
 *
 * 删除备份通用service实现类
 */
@Service
public class DelBackupsServiceImpl extends BasicServiceImpl<DelBackups> implements DelBackupsService {

    @Resource
    private DelBackupsMapper delBackupsMapper;

    @Override
    public BasicMapper<DelBackups> getBasicMapper() {
        return delBackupsMapper;
    }

    /**
     * @author LiYang
     * @date 20190522
     *
     * 还原删除的记录
     * @param delBackupsList
     * @throws Exception
     */
    @Override
    public void revert(List<DelBackups> delBackupsList) throws Exception {
        if (null == delBackupsList || delBackupsList.isEmpty()) {
            return;
        }
        JdbcTemplate jdbcTemplate = (JdbcTemplate) BeanTools.getBean(JdbcTemplate.class);
        for (int i = 0; i < delBackupsList.size(); i++) {
            DelBackups delBackup = delBackupsList.get(i);
            String revertSql = delBackup.getRevertSql();
            try {
                jdbcTemplate.update(revertSql);
                delBackup.setIsRevert("Y");
                delBackup.setRevertMsg("还原成功");
                delBackupsMapper.update(delBackup);
            } catch (Exception e) {
                delBackup.setIsRevert("N");
                delBackup.setRevertMsg(e.getMessage());
                delBackupsMapper.update(delBackup);
            }
        }
    }
}
